<?php
/**
 * Created by PhpStorm.
 * User: zeng
 * Date: 18-5-14
 * Time: 上午9:03
 */

namespace Lib;
//curl

class Common
{
    public static function http($url, $postfields, $method = 'POST', array $headers = array())
    {
        $ci = curl_init();
        /* Curl settings */
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ci, CURLOPT_ENCODING, 'gzip');
        curl_setopt($ci, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ci, CURLOPT_MAXREDIRS, 5);
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ci, CURLOPT_HEADER, false);

        switch (strtoupper($method)) {
            case 'POST':
                curl_setopt($ci, CURLOPT_POST, true);
                if (!empty($postfields)) {
                    if (is_array($postfields)) {
                        curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($postfields));
                    } else {
                        curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                    }
                }
                break;
            case 'DELETE':
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
                if (!empty($postfields)) {
                    $url = "{$url}?" . http_build_query($postfields);
                }
                break;
            case 'GET':
                if (!empty($postfields)) {
                    $url = "{$url}?" . http_build_query($postfields);
                }
                break;
        }

        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, true);

        $response = curl_exec($ci);
        curl_close($ci);
        return $response;
    }
    public static function log($data){
        file_put_contents('log.txt',$data.PHP_EOL,FILE_APPEND);
        file_put_contents('log.txt','--------'.date('Y-m-d H:i:s',time()).PHP_EOL,FILE_APPEND);
    }
}